//
//  CommentTableViewCell.m
//  JiuJiuShangCheng
//
//  Created by jiujiukeji on 2018/7/12.
//  Copyright © 2018年 jiujiukeji. All rights reserved.
//

#import "CommentTableViewCell.h"

@implementation CommentTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUpUI];
    }
    return self;
}
- (void)setUpUI
{
    WeakSelf
    self.star = [[MyStarView alloc] initWithFrame:CGRectMake(0, 0, 135, 35)];
    [self addSubview:_star];
    [_star mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self);
        make.size.mas_offset(CGSizeMake(135, 35));
    }];
    
    self.contentLB = [UILabel labelWithText:@"糖特别好吃，买了还几次，卖家人超好。推荐小朋友们都来尝试一下。" textColor:UIColorFromRGB(0x333333) fontSize:14];
    _contentLB.numberOfLines = 0;
    [_contentLB sizeToFit];
    [self addSubview:_contentLB];
    [_contentLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(rateWidth(24));
        make.top.equalTo(weakSelf.star.mas_bottom).offset(rateHeight(10));
        make.width.mas_lessThanOrEqualTo(@(rateWidth(670)));
    }];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 0;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, rateHeight(180), kScreenWidth, rateHeight(650)) collectionViewLayout:layout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = kClearColor;
    [_collectionView registerClass:[CommentCollectionViewCell class] forCellWithReuseIdentifier:@"collectionViewCell"];
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.scrollEnabled = NO;
    [self addSubview:_collectionView];

}
#pragma mark - collectionview delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imgArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CommentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionViewCell" forIndexPath:indexPath];
    [cell.commentImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http:%@",self.imgArray[indexPath.row]]]];
    
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((kScreenWidth-rateWidth(60)-rateWidth(30))/3, rateHeight(200));
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, rateWidth(30), 0, rateWidth(30));//分别为上、左、下、右
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.imgArray.count != 0) {
        CommentCollectionViewCell *cell = (CommentCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        // 浏览图片
        NSMutableArray *photos = [NSMutableArray array];
        for (NSString *string in self.imgArray) {
            [photos addObject:[NSString stringWithFormat:@"http:%@",string]];
        }
        [HPPhotoBrowser showFromImageView:cell.commentImg inView:[UIApplication sharedApplication].keyWindow withURLStrings:photos   atIndex:indexPath.row];
    }
}

@end
